package com.ctim.dao.user;

import com.ctim.model.user.Resource;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: lb on 2016/8/26.
 * Date:2016-08-26-13:57
 * desc：
 */
@Repository
public class ResourceDao {

    @Autowired
    @Qualifier("sqlSession")
    private SqlSessionTemplate sqlSession;

    public List<Resource> selectResourceByUserId(Integer userId, String type) {
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("type",type);
        return sqlSession.selectList("dta.resource.selectResourceByUserId",map);
    }


    public List<Resource> selectResourceByRole(Integer roleId) {
        return sqlSession.selectList("dta.resource.selectResourceByRole",roleId);
    }

    public List<Resource> selectMemuResource() {
        return sqlSession.selectList("dta.resource.selectMenuResource");
    }

    public List<Resource> selectButtoResource(Integer resourceId){
        return sqlSession.selectList("dta.resource.selectButtonResource",resourceId);
    }

    public int insertResource(Resource resource){
        return sqlSession.insert("dta.resource.insertResource",resource);
    }

    public int updateResource(Resource resource){
        return sqlSession.update("dta.resource.updateResource",resource);
    }

    public int deleteResource(Integer resourceId){
       return sqlSession.delete("dta.resource.deleteResource",resourceId);
    }

    public Resource selectResource(Resource resource) {
        return sqlSession.selectOne("dta.resource.selectResource", resource);
    }

    public int toggleEnable(Integer resourceId, Integer status, Integer updUid) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("resourceId", resourceId);
        map.put("status", status);
        map.put("updUid", updUid);
        return sqlSession.update("dta.resource.toggleEnable", map);
    }

    public List<Resource> getResources(Integer parentId) {
        return sqlSession.selectList("dta.resource.getResources", parentId);
    }

    public List<Resource> getMenus() {
        return sqlSession.selectList("dta.resource.getMenus");
    }

    public List<Resource> getUserResources(Integer userId) {
        return sqlSession.selectList("dta.resource.getUserResources", userId);
    }
}
